package com.gitee.kenewstar.migration.core;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kenewstar
 */
@Slf4j
@SuppressWarnings("all")
public class RequestParamInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 添加参数
        final Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, List<String>> paramMap = new HashMap<>(parameterMap.size());
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            paramMap.put(entry.getKey(), ListUtil.toList(entry.getValue()));
        }
        RequestParamHandler.addParamMap(paramMap);
        log.info("===================paramMap==========================> {}", JSONUtil.toJsonStr(paramMap));
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        RequestParamHandler.remove();
    }

}
